/**
 * @file CPvmSlaveCommands.cpp Implementácia triedy CPvmSlaveCommands.
 *
 * @author Martin Kosorinský.
 * @date 10.10.2009.
 */

#include "CPvmSlaveCommands.hpp"

void CPvmSlaveCommands::pvmSendDistPoint(int tid, epoint * P, big a, big b)   {

    if (point_at_infinity(P)) {
        cerr << "Error: '" << __FUNCTION__ << "': Procedura nepodporuje odosielanie infina!" << endl;
        return;
    }

    big x[4];
    x[0] = mirvar(1);
    x[1] = mirvar(1);
    x[2] = a;
    x[3] = b;

    epoint_get(P, x[0], x[1]);

    tracer << "Odosielam DistinguishedPoint: " << P << "\ta=" << a << "\tb=" << b << endl;

    pvm_initsend(PvmDataDefault);        // inicializaciaa send buffer
    pvmPackBig(x, 4, 1);                 // pole bigov natlacene do bufferu
    pvm_send(tid, PVM_ECDLP_DISTPOINT);  // odoslana sprava pre TID s kodom PVM_ECDLP_DISTPOINT

    mirkill(x[0]);
    mirkill(x[1]);
}

void CPvmSlaveCommands::pvmUnpackTaskPoint(epoint * P) {

    big y[2];
    y[0] = mirvar(0);
    y[1] = mirvar(0);

    //pvm_recv(tid, PVM_ECDLP_TASKPOINT);
    pvmUnpackBig(y, 2, 1);

    if (!epoint_set(y[0], y[1], 0, P)) {
        cerr << "Error: '" << __FUNCTION__ << "': Bod nelezi na krivke!" << endl;
        pvm_exit();
        exit(1);
    }

    mirkill(y[0]);
    mirkill(y[1]);
}

void CPvmSlaveCommands::pvmSendDL(int tid, big d) {

    tracer << "Odosielam DL: " << d << endl;

    pvm_initsend(PvmDataDefault);        // inicializaciaa send buffer
    pvmPackBig(&d, 1, 1);
    pvm_send(tid, PVM_ECDLP_DL);

}

void CPvmSlaveCommands::pvmUnpackECParams(big a, big b, big p, big o, big x, big y) {
    big z[6];
    z[0] = a;
    z[1] = b;
    z[2] = p;
    z[3] = o;
    z[4] = x;
    z[5] = y;

    pvmUnpackBig(z, 6, 1);
}

